﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class C001_BoardTests {
		private IWebDriver driver;
		private const String boardName = "My new board";
		private const String siteName = "The site name";
		private const String siteDescription = "Bleh";

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void EditBoard() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin");

			IWebElement link = driver.FindElement(By.LinkText("Edit"));
			link.Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));

			driver.InsertText("Name", "");

			link = driver.FindButton("Update");
			link.Click();

			Assert.AreEqual(String.Format("{0}forumadmin/board/update/1", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
			Assert.AreEqual(true, driver.PageSource.Contains("The board name is required!"));

			driver.InsertText("Name", "Test board");

			link = driver.FindButton("Update");
			link.Click();
			wait.Until(drv => drv.PageSource.Contains("The board was updated successfully!"));

			driver.VisitURL("forumadmin");
			wait.Until(drv => drv.PageSource.Contains("Edit"));

			Assert.AreEqual(true, driver.PageSource.Contains("Test board"));

			driver.LogOff();
		}

		[Test]
		public void CreateBoard() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(false, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(true, true, "No create button");
			}

			IWebElement link = driver.FindElement(By.LinkText("Add Board"));
			link.Click();

			try {
				// Create pop-up should be visible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(true, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(false, true, "No create button found");
			}

			IWebElement closeLink = driver.FindElement(By.LinkText("Close"));
			closeLink.Click();

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(false, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(true, true, "No create button");
			}

			link.Click();

			IWebElement createLink = driver.FindElement(By.LinkText("Create"));
			createLink.Click();

			Assert.AreEqual(true, driver.PageSource.Contains("The board name is required!"), "The error: \"board name required\" is shown");
			Assert.AreEqual(false, driver.PageSource.Contains("A new board was created successfully!"), "The board was create without required values.");

			driver.InsertText("Name", boardName);
			driver.InsertText("Description", siteDescription);

			createLink.Click();
			driver.Wait(1);

			Assert.AreEqual(true, driver.PageSource.Contains("A new board was created successfully!"), "Created successfully text displayed");

			driver.VisitURL("forumadmin","stats");

			IWebElement row = driver.FindBoardRow(boardName);
			Assert.AreEqual(true, row != null, "Find new board row");

			IWebElement editLink = row.FindElement(By.LinkText("Edit"));
			editLink.Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));
			driver.Wait(1);

			Assert.AreEqual(boardName, driver.GetTextBoxValueByName("Name"), "Board name");
			Assert.AreEqual(siteDescription, driver.GetTextAreaValueByName("Description"), "Site description");

			driver.LogOff();
		}

		[Test]
		public void DeleteBoard() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			IWebElement row = driver.FindBoardRow(boardName);
			Assert.AreEqual(true, row != null, "The board was not found");

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("OK"));
				Assert.AreEqual(false, true);
			}
			catch {
				Assert.AreEqual(true, true);
			}

			IWebElement deleteLink = row.FindElement(By.LinkText("Delete"));
			deleteLink.Click();

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("OK"));
				Assert.AreEqual(true, true);
			}
			catch {
				Assert.AreEqual(false, true);
			}

			IWebElement link = driver.FindElement(By.LinkText("OK"));
			link.Click();

			driver.VisitURL("forumadmin", "stats");

			Assert.AreEqual(false, driver.PageSource.Contains(boardName));

			driver.LogOff();
		}
	}
}